package edu.upf.opencities.service;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import edu.upf.opencities.datamodel.Message;
import edu.upf.opencities.datamodel.ObjectFactory;
import edu.upf.opencities.datamodel.User;
import edu.upf.opencities.manager.UserManager;


/**
 * 
 * @author Manu (manuelpalacin@gmail.com)
 *
 * REST (Only Jersey) (CXF: http://cxf.apache.org/docs/jaxrs-services-description.html#JAXRSServicesDescription-Basicexample)
 * WADL descriptor: http://[SERVER_ADDRESS]:[SERVER_PORT]/OSN/rest/application.wadl
 * 
 * REST request
 * http://[SERVER_ADDRESS]:[SERVER_PORT]/OSN/rest/[ws_path_name]/[method_path_name]/{param1}/.../{paramN}
 * 
 */

@Service("user")
@Path("/user")
public class UserService {

	@Autowired
	private UserManager userManager;
	@Autowired
	private ObjectFactory objectFactory;
	
	
	@GET
	@Path("/echo/{param}")
    public String echo(@PathParam("param")String param){
    	return "Echo: "+param;
    }
    
	/**
	 * Service register User method
	 * 
	 * @param userName
	 * @param userLastName
	 * @param mail
	 * @param userPassword
	 * @param company
	 * @param country
	 * 
	 * @return
	 */
	//http://localhost:8080/OSN/rest/user/registerUser/Manuel/Palacin/manuel.palacin@upf.edu/s3cr3t/UPF/Spain
	@GET
	@Path("/registerUser/{userName}/{userLastName}/{mail}/{userPassword}/{company}/{country}")
	public Message registerUser(@PathParam("userName")String userName, @PathParam("userLastName")String userLastName, 
			@PathParam("mail")String mail, @PathParam("userPassword")String userPassword, 
			@PathParam("company")String company, @PathParam("country")String country) {
		
		return createMessage(String.valueOf(userManager.registerUser(userName, userLastName,
				mail, false, userPassword, company, country)));

	}
	
	/**
	 * Service update User method
	 * 
	 * @param userName
	 * @param userLastName
	 * @param mail
	 * @param userPassword
	 * @param company
	 * @param country
	 * 
	 * @return
	 */
	//http://localhost:8080/OSN/rest/user/updateUser/Manuel/Palacin/manuel.palacin@upf.edu/s3cr3t/UPF/Spain
	@GET
	@Path("/updateUser/{userName}/{userLastName}/{mail}/{userPassword}/{company}/{country}")
	public Message updateUser(@PathParam("userName")String userName, @PathParam("userLastName")String userLastName, 
			@PathParam("mail")String mail, @PathParam("userPassword")String userPassword, 
			@PathParam("company")String company, @PathParam("country")String country) {
		
		return createMessage(String.valueOf(userManager.updateUser(userName, userLastName,
				mail, false, userPassword, company, country)));

	}
	
	
	/**
	 * Service get User method
	 * 
	 * @param mail
	 * @return
	 */
	//http://localhost:8080/OSN/rest/user/getUser/manuel.palacin@upf.edu
	@GET
	@Path("/getUser/{mail}")
	public User getUser(@PathParam("mail")String mail){	
		return userManager.getUser(mail);
	}
	
	/**
	 * Service delete User method
	 * 
	 * @param mail
	 * @param userPassword
	 */
	//http://localhost:8080/OSN/rest/user/deleteUser/manuel.palacin@upf.edu/s3cr3t
	@GET
	@Path("/deleteUser/{mail}/{userPassword}")
	public void deleteUser(@PathParam("mail")String mail,@PathParam("userPassword")String userPassword){	
		userManager.deleteUser(mail, userPassword);
	}
	
	/**
	 * Service login User method
	 * 
	 * @param mail
	 * @param userPassword
	 * @return
	 */
	//http://localhost:8080/OSN/rest/user/loginUser/manuel.palacin@upf.edu/s3cr3t
	@GET
	@Path("/loginUser/{mail}/{userPassword}")
	public Message loginUser(@PathParam("mail")String mail,@PathParam("userPassword")String userPassword ){	
		return createMessage(String.valueOf(userManager.loginUser(mail, userPassword)));
	}
	
	public Message createMessage(String content){
		
		Message message = objectFactory.createMessage();
		message.setContent(content);
		return message;
	}
}
